package com.csr;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

public class GetPersonalInto implements Runnable
{
	MD5Generator md5Generator=new MD5Generator();
	HttpConn httpConn=new HttpConn();
	MyAccountInfo myAccountInfo;
	ProgressDialog pd;
	Handler handler;
	Context context;
	GetPersonalInto(Context context,Handler handler, ProgressDialog pd)
	{
		this.context=context;
		this.handler=handler;
		this.pd=pd;
		
	}
	
	public void run() 
	{
		// TODO Auto-generated method stub
		try
		{
			String checksum=md5Generator.MD5(md5Generator.SHA1(AppData.getUserInfo().getLoginID()+"|"+
						 	AppData.getUserInfo().getpublickey() ));
			 String URL=AppData.hostURL+"/WebService/iTopUp/csrapp.php/GetPersonalInfo/LoginId/"+AppData.getUserInfo().getLoginID()
                     +"/Checksum/"+checksum;
          System.out.println("URL===>\n"+URL);
          String response=httpConn.createHttpConn(URL);
          System.out.println("Response:"+response);
          myAccountInfo=new MyAccountInfo();
          final JSONObject jsonObject=new JSONObject(response);

          if(jsonObject.getString("ResponseCode").equals("000"))
          {
               
               
               if(jsonObject.has("Balance"))
                    myAccountInfo.setBalance(""+Double.parseDouble(jsonObject.getString("Balance"))/100);
               else
                   myAccountInfo.setBalance("Not Avilable");

                
               handler.post(new Runnable()
               { 
					public void run() 
					{
						// TODO Auto-generated method stub
						Toast.makeText(context,"Your Account Balance is "+myAccountInfo.getBalance(),Toast.LENGTH_SHORT).show();
					    pd.dismiss();
					}
               }); 
          }
          else
          {
        	  handler.post(new Runnable()
        	  {
				
				public void run() 
				{
					// TODO Auto-generated method stub
					pd.dismiss();
					try 
					{
						Toast.makeText(context,jsonObject.getString("ResponseDescription"),Toast.LENGTH_SHORT).show();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
          }

		}//try
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
	}

}
